/**
 * <br>项目名: fastdfs-api
 * <br>文件名: UploadCtrl.java
 * <br>Copyright 2015 恒昌互联网运营中心
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName: NewTestUpload
 * @Description: 文件上传下载
 * @author zhaoc
 * @date 2015年6月17日
 * @time  上午11:02:22
 */
@SuppressWarnings("Duplicates")
public class SCSProductUpload {
	private Logger log = LoggerFactory.getLogger(SCSProductUpload.class);

	private static String UPLOAD_PATH;
	private static String DOWNLOAD_PATH;

	static {
//		UPLOAD_PATH = "http://172.19.0.146/fs-api/uploadFile.hc";//预发布
//		UPLOAD_PATH = "http://192.168.100.70/fs-api/uploadFile.hc";/*测试环境*/
//		DOWNLOAD_PATH = "http://filesys-test.hengchang6.com/fs-api/downloadFile.hc";/*测试环境*/
//		DOWNLOAD_PATH = "http://192.168.100.70/fs-api/downloadFile.hc";/*测试环境*/
		//工单系统,实际为192.168.100.70
		//UPLOAD_PATH = "http://111.204.230.5:18080/fs-api/uploadFile.hc";

		UPLOAD_PATH = "http://118.26.169.228/fs-api/uploadFile.hc";//生产环境
	}

	@Test
	public void test() throws Exception {
		List<String> fileNameList = new ArrayList<>();
//		fileNameList.add("陈德英-2072329530752.pdf");
//		fileNameList.add("段增恒-2099687714176.pdf");
//		fileNameList.add("顾宝珠-2102759156224.pdf");
//		fileNameList.add("李秀君-2061179197952.pdf");
//		fileNameList.add("吕永华-2067265869184.pdf");
//		fileNameList.add("吕永华-2067327838720.pdf");
//		fileNameList.add("马慧娟-2088907833856.pdf");
//		fileNameList.add("马慧娟-2093680887168.pdf");
//		fileNameList.add("马晓东-2067490370944.pdf");
//		fileNameList.add("任国华-2112083312000.pdf");
//		fileNameList.add("王玉华-2055171829120.pdf");
//		fileNameList.add("朱建英-2107193316736.pdf");
//		fileNameList.add("CFCA数字证书服务协议-000.pdf");
		fileNameList.add("多乐融注册协议-000.pdf");
		for(int i = 0; i < fileNameList.size(); i++) {
			upload(fileNameList.get(i));
		}

	}

	public static void upload(String realFileName) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			// 要上传的文件的路径
			String filePath = "/Users/zhangjin/temp/重签委托划扣协议-新/";
			// 把一个普通参数和文件上传给下面这个地址 是一个servlet
			HttpPost httpPost = new HttpPost(UPLOAD_PATH);
			// 把文件转换成流对象FileBody
			File file = new File(filePath+realFileName);
			FileBody bin = new FileBody(file);
			//调用者业务ID
			String lendId = StringUtils.substringBetween(realFileName, "-", ".pdf");
			StringBody businessId = new StringBody(lendId, ContentType.create("text/plain", Consts.UTF_8));
			//调用者系统ID
			StringBody systemId = new StringBody("SCS", ContentType.create("text/plain", Consts.UTF_8));
			//文件类型(注* 此处上传pdf类型文件，fileType参数填写为 doc)
			StringBody fileType = new StringBody("doc", ContentType.create("text/plain", Consts.UTF_8));
			//文件原始名称
			StringBody fileName = new StringBody(realFileName, ContentType.create("text/plain", Consts.UTF_8));
			//从文件名，非图片类型不需要裁剪 缩放的文件 此参数为空
			StringBody slaveName = new StringBody("", ContentType.create("text/plain", Consts.UTF_8));
			//以浏览器兼容模式运行，防止文件名乱码。
			HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
					.addPart("file", bin)
					.addPart("systemId", systemId)
					.addPart("fileType", fileType)
					.addPart("fileName", fileName)
					.addPart("slaveName", slaveName)
					.addPart("businessId", businessId).setCharset(CharsetUtils.get("UTF-8")).build();//请求编码使用UTF-8

			httpPost.setEntity(reqEntity);

			System.out.println("发起请求的页面地址 " + httpPost.getRequestLine());
			// 发起请求 并返回请求的响应
			CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
			try {
				System.out.println("----------------------------------------");
				// 打印响应状态
				System.out.println(httpResponse.getStatusLine());
				// 获取响应对象
				HttpEntity resEntity = httpResponse.getEntity();
				if (resEntity != null) {
					// 打印响应长度
					System.out.println("Response content length: "+ resEntity.getContentLength());
					// 打印响应内容
					String json=EntityUtils.toString(resEntity, Charset.forName("UTF-8"));
					System.out.println(json);
					JSONObject jsonObject = JSON.parseObject(json);
					JSONObject data = jsonObject.getJSONObject("data");
					String fsId = data.getString("fsId");

					System.out.println(String.format("lendId:%s,fsId:%s",lendId,fsId));
				}
				// 销毁
				EntityUtils.consume(resEntity);
			} finally {
				httpResponse.close();
			}
		} finally {
			httpClient.close();
		}
	}





}
